﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyPlayer
{
    class MyContainer<T> : IMyContainer<T>
    {
        LinkedList<T> _list;

        public MyContainer(LinkedList<T> list)
        {
            _list = list;
        }

        public void addLast(T value)
        {
            _list.AddLast(value);
        }

        public void addFirst(T value)
        {
            _list.AddFirst(value);
        }

        public LinkedList<T> getList()
        {
            return _list;
        }

        public bool equals(IMyContainer<T> myContainer)
        {
            return (myContainer == this);
        }

        public delegate int CompareDelegate(T a, T b);

        public bool sort(CompareDelegate cmp)
        {
            try
            {
                LinkedList<T> list = new LinkedList<T>();

                for (int i = 0; i < (_list.Count - 1); i++)
                {
                    T e1 = _list.ElementAt(i);
                    T e2 = _list.ElementAt(i + 1);

                    int a = cmp.Invoke(e1, e2);

                    if (a > 0)
                    {
                        list.AddLast(e2);
                        list.AddLast(e1);
                    }
                    else
                    {
                        list.AddLast(e1);
                        list.AddLast(e2);
                    }
                }

                _list = list;

                return true;
            }
            catch 
            { 
                return false; 
            }
        }
    }
}
